package com.example.taobaounion.ui.fragment

import android.content.Intent
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.taobaounion.R
import com.example.taobaounion.base.BaseFragment
import com.example.taobaounion.model.bean.SelectCategoryBean
import com.example.taobaounion.model.bean.SelectCategoryContentBean
import com.example.taobaounion.presenter.impl.SelectPresenterImpl
import com.example.taobaounion.view.ISelectCallBack
import com.example.taobaounion.ui.activity.DiscountTicketActivity
import com.example.taobaounion.ui.adapter.SelectCategoryAdapter
import com.example.taobaounion.ui.adapter.SelectCategoryContentAdapter
import com.example.taobaounion.utils.PresenterManager

/**
 *@author : Administrator
 *@descreption : 主界面
 */
class SelectFragment : BaseFragment(), ISelectCallBack, SelectCategoryAdapter.SelectCategoryItemOnClickListener, SelectCategoryContentAdapter.SelectCategoryContentItemOnClickListener {
    private lateinit var mCategoryRecyclerViewAdapter: SelectCategoryAdapter
    private lateinit var mCategoryRecyclerView: RecyclerView
    private lateinit var mCategoryContentRecyclerViewAdapter: SelectCategoryContentAdapter
    private lateinit var mCategoryContentRecyclerView: RecyclerView
    private var mSelectPresenterImpl: SelectPresenterImpl? = null
    private var isFirstIn = true


    override fun setLayoutResourcesID(): Int {
        return R.layout.fragment_select
    }

    override fun initView(container: View) {

        //分类recyclerview的初始化
        mCategoryRecyclerView = container.findViewById(R.id.categoryRecyclerView)
        mCategoryRecyclerView.layoutManager = LinearLayoutManager(context)
        mCategoryRecyclerViewAdapter = SelectCategoryAdapter()
        mCategoryRecyclerViewAdapter.setSelectCategoryItemOnClickListener(this)
        mCategoryRecyclerView.adapter = mCategoryRecyclerViewAdapter

        //分类内容recyclerview的初始化
        mCategoryContentRecyclerView = container.findViewById(R.id.contentRecyclerView)
        mCategoryContentRecyclerView.layoutManager = LinearLayoutManager(context)
        mCategoryContentRecyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                //super.getItemOffsets(outRect, view, parent, state)
                outRect.top = 5
                outRect.bottom = 5
            }
        })
        mCategoryContentRecyclerViewAdapter = SelectCategoryContentAdapter()
        mCategoryContentRecyclerViewAdapter.setSelectCategoryItemOnClickListener(this)
        mCategoryContentRecyclerView.adapter = mCategoryContentRecyclerViewAdapter

    }

    override fun initData() {
        mSelectPresenterImpl?.registerCallBack(this)
        mSelectPresenterImpl?.getCategory()
        setViewState(State.LOADING)
    }


    override fun setPresenter() {
        mSelectPresenterImpl = PresenterManager.getInstance().selectPresenterImpl
    }

    override fun getDataSuccess(body: SelectCategoryBean?) {
        if (body != null) {
            setViewState(State.SUCCESS)
            mCategoryRecyclerViewAdapter.setData(body.data)
            if (isFirstIn) {
                mSelectPresenterImpl?.getCategoryContent(body.data[0])
                isFirstIn = false
            }
        }

    }

    override fun getDataError() {
    }

    override fun loading() {
    }

    override fun getContentDataSuccess(data: SelectCategoryContentBean.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean?) {
        if (data?.uatm_tbk_item == null) return
        mCategoryContentRecyclerViewAdapter.setData(data?.uatm_tbk_item)
    }

    override fun getContentDataError() {
    }

    override fun loadingContentData() {
    }

    override fun onDestroy() {
        super.onDestroy()
        mSelectPresenterImpl?.unRegisterCallBack()
    }

    override fun selectCategoryItemOnClick(dataBean: SelectCategoryBean.DataBean) {
        mSelectPresenterImpl?.getCategoryContent(dataBean)
    }

    override fun contentItemOnClick(dataBean: SelectCategoryContentBean.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean) {
        if (dataBean.coupon_click_url != null) {
            PresenterManager.getInstance().discountTicketPresenterImpl.getDiscountTicket(dataBean.coupon_click_url, dataBean.title, dataBean.pict_url)
        } else {
            PresenterManager.getInstance().discountTicketPresenterImpl.getDiscountTicket(dataBean.click_url, dataBean.title, dataBean.pict_url)
        }
        val intent = Intent(context, DiscountTicketActivity::class.java)
        intent.putExtra("title", dataBean.title)
        startActivity(intent)
    }


}